<template>
  <div>
    <el-form
      ref="addFormRef"
      :rules="rules"
      :model="addForm.data"
      label-width="120"
    >
      <el-form-item label="泊位编号" prop="code">
        <el-input
          v-model="addForm.data.code"
          style="width: 320px"
          placeholder="请输入泊位编号"
          maxlength="10"
        />
      </el-form-item>
      <el-form-item label="所属行政区" prop="reginId">
        <el-cascader
          v-model="addForm.data.reginId"
          :options="reginId.data"
          filterable
          style="width: 320px"
          placeholder="请选择所属行政区"
          :props="{
            value: 'id',
            label: 'name',
            children: 'nodes',
          }"
          @change="handleCascaderChange"
          clearable
        />
      </el-form-item>
      <el-form-item label="所属路段" prop="sectionId">
        <el-select
          v-model="addForm.data.sectionId"
          filterable
          style="width: 320px"
          placeholder="请选择所属路段"
        >
          <el-option
            v-for="item in sectionId.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="status">
        <el-select
          v-model="addForm.data.status"
          filterable
          style="width: 320px"
          placeholder="请选择使用状态"
        >
          <el-option
            v-for="item in status.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="泊位类型" prop="type">
        <el-select
          v-model="addForm.data.type"
          filterable
          style="width: 320px"
          placeholder="请选择泊位类型"
        >
          <el-option
            v-for="item in type.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序方式" prop="stay">
        <el-radio-group v-model="addForm.data.stay">
          <el-radio v-for="item in stay.data" :key="item.id" :value="item.id">{{
            item.name
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)"
              >重 置</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import {
  getCanton,
  getDictionary,
  getRoadList,
} from "@/service/modules/publicModule";
import { getAddBerth } from "../request.js";

const reginId = reactive({
  data: [],
});

const sectionId = reactive({
  data: [],
});

const status = reactive({
  data: [],
});

const type = reactive({
  data: [],
});

const stay = reactive({
  data: [
    {
      id: 1,
      name: "靠左",
    },
    {
      id: 2,
      name: "靠右",
    },
  ],
});

const getData = async () => {
  await getCanton().then((res) => {
    reginId.data = res.data.result;
  });
  await getDictionary("berth_status").then((res) => {
    status.data = res.data.result;
  });
  await getDictionary("road_berth_type").then((res) => {
    type.data = res.data.result;
  });
  await getRoadList(addForm.data.reginId).then((res) => {
    sectionId.data = res.data.result;
  });
};
getData();

const addFormRef = ref("");

const addForm = reactive({
  data: {
    reginId: "",
  },
});

const handleCascaderChange = async (value) => {
  console.log(value);
  sectionId.data = [];
  if (value !== null && value.length > 0) {
    addForm.data.reginId = value[value.length - 1];
  } else {
    addForm.data.reginId = "";
  }
  await getRoadList(addForm.data.reginId).then((res) => {
    sectionId.data = res.data.result;
  });
};

const rules = reactive({
  code: [{ required: true, message: "请输入泊位编号", trigger: "blur" }],
  reginId: [{ required: true, message: "请选择所属行政区", trigger: "change" }],
  sectionId: [{ required: true, message: "请选择所属路段", trigger: "change" }],
  status: [{ required: true, message: "请选择使用状态", trigger: "change" }],
  type: [{ required: true, message: "请选择泊位类型", trigger: "change" }],
  stay: [{ required: true, message: "请选择排序方式", trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getAddBerth(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = async (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  await getRoadList().then((res) => {
    sectionId.data = res.data.result;
  });
  formEl.resetFields();
};
const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>